package caiziyou.com.designpattern.adapter.springmvc;

import java.util.ArrayList;
import java.util.List;

/**
 * 符合设计模式的OCP原则
 * @author Administrator
 *
 */
public class DispatchServlet {
	
	public static List<HandlerAdapter> handlerAdapters = new ArrayList<>();
	
	public DispatchServlet() {
		handlerAdapters.add(new SimpleHandlerAdapter());
		handlerAdapters.add(new HTTPHandlerAdapter());
	}

	public void doDispatch(Controller controller) {
		HandlerAdapter ha = getHandle(controller);
		ha.handle(controller);
	}
	
	private HandlerAdapter getHandle(Controller controller) {
		for (HandlerAdapter handlerAdapter : handlerAdapters) {
			if (handlerAdapter.supports(controller)) {
				return handlerAdapter;
			}
		}
		System.out.println("没有对应的适配器");
		return null;
	}
}
